import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mes_app/common/index.dart';
import 'package:get/get.dart';
import 'package:validatorless/validatorless.dart';

import 'index.dart';

class LoginPage extends GetView<LoginController> {
  const LoginPage({super.key});

  // 主视图
  Widget _buildView(BuildContext context) {
    return Container(
      width: double.infinity, // 宽度无限大，以适应父组件
      decoration: const BoxDecoration(
        image: DecorationImage(
          image: AssetImage(
            AssetsImages.logoBackgroundPng,
          ),
          fit: BoxFit.fill, // 填充整个界面
        ),
      ),
      child: SafeArea(
        child: <Widget>[
          _pageTitle(context).paddingTop(100.w),
          _buildForm(context).card(
            color: context.colors.scheme.surface,
            margin: EdgeInsets.only(top: 34.w),
            shape: const RoundedRectangleBorder(
              borderRadius: BorderRadius.all(
                Radius.circular(10),
              ),
            ),
          ),
        ]
            .toColumn(crossAxisAlignment: CrossAxisAlignment.start)
            .paddingHorizontal(AppSpace.page),
      ),
    );
  }

  // 头部标题
  Widget _pageTitle(BuildContext context) {
    var colorScheme = context.colors.scheme;
    return <Widget>[
      TextWidget.h3(
        LocaleKeys.loginBackTitle.tr,
        color: colorScheme.onPrimary,
      ),
      TextWidget.h3(
        LocaleKeys.loginBackDesc.tr,
        color: colorScheme.onPrimary,
      ),
    ].toColumn(
      crossAxisAlignment: CrossAxisAlignment.start,
    );
  }

  // 表单页
  Widget _buildForm(BuildContext context) {
    return Form(
      key: controller.formKey, //设置globalKey，用于后面获取FormState
      autovalidateMode: AutovalidateMode.onUserInteraction,
      child: <Widget>[
        // username
        InputFormFieldWidget(
          controller: controller.userNameController,
          labelText: LocaleKeys.loginBackFieldUsername.tr,
          placeholder: LocaleKeys.loginBackFieldUsernamePlaceholder.tr,
          prefix: const Icon(Icons.person),
          validator: Validatorless.required(LocaleKeys.validatorRequired.tr),
          // suffix: const Icon(Icons.done),
        ).paddingBottom(AppSpace.listRow.w),

        // password
        InputFormFieldWidget(
          controller: controller.passwordController,
          labelText: LocaleKeys.loginBackFieldPassword.tr,
          obscureText: true,
          placeholder: LocaleKeys.loginBackFieldPasswordPlaceholder.tr,
          prefix: const Icon(Icons.lock),
          validator: Validatorless.required(LocaleKeys.validatorRequired.tr),
          // suffix: const Icon(Icons.done),
        ).paddingBottom(AppSpace.listRow.w * 2),

        // 登录按钮
        ButtonWidget.primary(
          LocaleKeys.loginSignIn.tr,
          onTap: controller.onSignIn,
          scale: WidgetScale.large,
        ).width(double.infinity).paddingBottom(30.w),
      ].toColumn().paddingVertical(10),
    ).paddingAll(AppSpace.card);
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LoginController>(
      init: LoginController(),
      id: "login",
      builder: (_) {
        return Scaffold(
          resizeToAvoidBottomInset: false,
          body: _buildView(context),
        );
      },
    );
  }
}
